require 'yaml'

class GameConfig
  def self.load_options
    load_default_options
    begin
      source = File.open("config.yaml")
      yp = YAML::load_documents(source) do |opts|
        opts.each do |key, val|
          @@options[key] = val
        end
      end
    rescue
      DebugOut.out "An error occurred while loading config file (the config file probably doesn't exist); using default config"
    end
  end
  
  def self.load_default_options
    @@options = { "unit0_uniform_color" => 0xffff6500, "unit0_skin_color" => 0xff888877, "unit0_health" => 100, "unit0_controller" => "Human (right)", "unit0_team" => 1,
                  "unit1_uniform_color" => 0xff7f0000, "unit1_skin_color" => 0xff888877, "unit1_health" => 100, "unit1_controller" => "Computer", "unit1_team" => 1,
                  "unit2_uniform_color" => 0xff0094ff, "unit2_skin_color" => 0xff888877, "unit2_health" => 100, "unit2_controller" => "Computer", "unit2_team" => 2,
                  "unit3_uniform_color" => 0xff007f0e, "unit3_skin_color" => 0xff888877, "unit3_health" => 100, "unit3_controller" => "Computer", "unit3_team" => 2,
                  "unit4_uniform_color" => 0xff656565, "unit4_skin_color" => 0xff888877, "unit4_health" => 100, "unit4_controller" => "None", "unit4_team" => 5,
                  "unit5_uniform_color" => 0xff656565, "unit5_skin_color" => 0xff888877, "unit5_health" => 100, "unit5_controller" => "None", "unit5_team" => 6,
                  "unit6_uniform_color" => 0xff656565, "unit6_skin_color" => 0xff888877, "unit6_health" => 100, "unit6_controller" => "None", "unit6_team" => 7,
                  "unit7_uniform_color" => 0xff656565, "unit7_skin_color" => 0xff888877, "unit7_health" => 100, "unit7_controller" => "None", "unit7_team" => 8,
                  "point_limit" => 5, "sound_volume" => 1, "screen_width" => 800, "screen_height" => 600, "fullscreen" => "No", "gibbing" => "No" }
  end
  
  def self.save_options_to_file
    File.open("config.yaml", "w") do |f|
      f.puts "---"
      @@options.each do |key, val|
        val = "\"#{val}\"" if ["Yes","No"].include?(val)
        f.puts "#{key}: #{val}"
      end
    end
  end
  
  def self.[](key)
    @@options[key]
  end
  
  def self.[]=(key, val)
    @@options[key] = val
  end
end
